// pages/shopStore/goodsEdit.js
var app = getApp();
var api = require('../../utils/api.js');
var rsa = require('../../utils/rsa.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    array: ['固定金额', '固定比例'],
    index: 0,

    gnumber: 1,
    ggflag: [0],
    topclassify: [], //一级目录
    topclassindex: 0,

    subclassify: [], //二级目录
    subclassindex: 0,

    sunits: ['个', '斤', '包','两', '克', '千克', '公斤', '桶', '提', '卷', '条', '打', '件', '只', '瓶', '袋', '箱', '双', '盒', '双', '台', '块', '套', '把', '码'],
    unitIndex: 0,

    ecode: '', //商品条码

    sizeClass: [],  //vip价格数据
    sizeIndex: 0,
    itemFlag1: false,
    sizevalue: '',
    sizeArr: [], //价格数组

    oprice: '', //成本价
    lprice: '', //零售价
    hprice: '', //会员价
    pprice: '', //批发价

    goodName: '', //商品名称
    rememberCode: '', //商品助记码
    weightUnit: '', //称重计量

    diliveryArr: ['固定金额', '固定比率'],
    dindex: 0,//提成方式索引
    dliveryValue: ''  //提成
  },
  //提交
  uploadAction: function () {
    var that = this;
    const topIndex = this.data.topclassindex;
    const secIndex = this.data.subclassindex;
    var topclassify = this.data.topclassify;
    var seClassify = this.data.subclassify;
    const shopNum = app.globalData.shoopnum;
    const busNum = app.globalData.merchno;

    

    const gname = this.data.goodName;
    const gcode = this.data.rememberCode;
    var topCategary = topclassify[topIndex];
    var secCategary = seClassify[secIndex];
    var topClassid = '';  //一级id
    var topclassName = ''; //一级名称
    var seClassid = ''; //二级id
    var seClassName = ''; //二级名称

    if (topCategary) {
      topClassid = topCategary.cat_id.toString();
      topclassName = topCategary.cat_name
    } else {
      return;
    }
    //vip价格数组
    var vip_prices = [];
    const vipprice = this.data.sizeArr; //[{pid:'',mval:123}];
    const vippricesize = this.data.sizeClass;
    for(var i = 0;i<vipprice.length;i++){
      var mid = vippricesize[i].member_lv_id;
      var vprice = vipprice[i].mval;
      var temppci = (parseFloat(vprice) * 100).toFixed(0);
      var temdoc = { 'member_lv_id': mid,'price':temppci};
      vip_prices.push(temdoc);
    }
    const weight = this.data.weightUnit;

    const lsunits = this.data.sunits;
    const lunitIndex = this.data.unitIndex;

    var bweight = lsunits[lunitIndex];  //基本单位

    var productCode = this.data.ecode;

  
    var price1 = (parseFloat(this.data.oprice)*100).toFixed(0);
    var price2 =  (parseFloat(this.data.lprice) * 100).toFixed(0);
    var price3 = (parseFloat(this.data.hprice) * 100).toFixed(0);
 
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.add', "seller_bn": shopNum, 'session_id': settion, 'save_type': 'add', "barcode": productCode, "remeber_code": gcode, "good_bn": '', "name": gname, "cat_id": topClassid, "cost": price1, "price": price2, "member_lv_price": vip_prices, 'unit': bweight
    }, (err, res) => {
      if(err){
        wx.showModal({
          title: '提示',
          content: err,
          showCancel: false,
          success: function (res) {
          }
        })
        return
      }
      if (res.return_code =='0000'){
        wx.showModal({
          title: '成功',
          content: '是否继续添加',
          showCancel: true,
          success: function (res) {
            if (res.confirm) {
              that.setData({
                goodName: '',
                rememberCode: '',
                ecode: '',
                oprice: '', //成本价
                lprice: '', //零售价
                hprice: '', //会员价
                sizeArr:'',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        wx.showModal({
          title: '提示',
          content: res.return_msg + ',返回上个界面',
          showCancel: false,
          success: function (res) {
            
          }
        })
      } 
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    this.checkHightClassify();
  },
  onLoad: function (options) {
    this.viplevel();
  },
  //查询会员等级
  viplevel:function(){
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, { 'method': 'member.lvlist', "seller_bn": shopnum, "session_id": settion }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          duration: 3000,
        })
        return
      }
      var that = this;
      that.setData({
        sizeClass: res.data.list,
      })
    });
  },
  //查询一级商品类目
  checkHightClassify: function () {
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {'method':'goods.cat_list',"seller_bn": shopnum, "session_id": settion}, (err, res) => {
      if(err){
        wx.showToast({
          title: err,
          duration:3000,
        })
        return
      }
      var listArr = res.data.list;
      var that = this;
     
       var topc = listArr;
       const addnew = { cat_id: '', cat_name: '--添加分类--' };
        topc.push(addnew);
        that.setData({
          topclassify: topc,
        })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //删除规格
  redeceAction: function (e) {
    const index = e.currentTarget.dataset.info;
    var cunrent = this.data.sizeArr;
    cunrent.splice(index, 1);
    this.setData({
      sizeArr: cunrent,
    })
  },
  deletAction: function (e) {
    var cunrent = this.data.sizeArr;
    var value = this.data.sizevalue;
    var index = this.data.sizeIndex;
    var predic = { pid: index, mval: '' };
    if (value) {
      predic.mval = value;
      cunrent.push(predic);
    } else {
    }
    this.setData({
      itemFlag1: false,
      sizeArr: cunrent,
    })
  },
  //规格输入框
  inputAction: function (e) {
    const value = e.detail.value;
    this.setData({
      sizevalue: value,
    })
  },
  showggAction: function (e) {
    this.setData({
      itemFlag1: true,
      sizevalue: '',
    })
  },

  //一级类目
  bindTopClassPickerChange: function (e) {
    const vl = e.detail.value;
    const toparr = this.data.topclassify;
    if (vl == toparr.length - 1) {
      wx.navigateTo({
        url: '/pages/storer/goodcategaryadd',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      const current = toparr[vl].classify_id;
      this.setData({
        topclassindex: vl
      })
      this.checkSubClass(current);
    }
  },
  //二级类目
  bindSubClassPickerChange: function (e) {
    const vl = e.detail.value;
    this.setData({
      subclassindex: vl
    })
  },
  bindUnitsPickerChange: function (e) {
    this.setData({
      unitIndex: e.detail.value
    })
  },
  //输入商品条码
  ecodeMakesure: function (e) {
    this.setData({
      ecode: e.detail.value,
    })
  },
  //扫码
  scanCode: function () {
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        this.setData({
          ecode: res.result,
        })
      }
    })
  },
  //商品规格选择
  bindGGPickerChange: function (e) {
    this.setData({
      sizeIndex: e.detail.value,
    })
  },
  //商品名称
  goodNameAction: function (e) {
    this.setData({
      goodName: e.detail.value,
    })
  },
  //商品助记码
  rememberAction: function (e) {
    this.setData({
      rememberCode: e.detail.value,
    })
  },
  //称重计量
  radioChange: function (e) {
    this.setData({
      weightUnit: e.detail.value,
    })
  },
  //提成额度
  ticAction: function (e) {
    this.setData({
      dliveryValue: e.detail.value,
    })
  },
  // oprice: '', //成本价
  // lprice: '', //零售价
  // hprice: '', //会员价
  // pprice: '', //批发价
  //成本价
  coastAction:function(e){
    this.setData({
      oprice:e.detail.value,
    })
  },
  //零售价
  sellAction: function (e) {
    this.setData({
      lprice: e.detail.value,
    })
  },
  //会员价
  hyAction: function (e) {
    this.setData({
      hprice: e.detail.value,
    })
  },
  //批发价
  batchAction: function (e) {
    this.setData({
      pprice: e.detail.value,
    })
  }

})